package cn.wx.read.neo4j.repository;

import java.util.List;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;

import cn.wx.read.neo4j.entity.Neo4jExpert;
import cn.wx.read.neo4j.entity.Relation;

public interface Neo4jExpertRepository extends Neo4jRepository<Neo4jExpert,Long>{
	
	/**
	 * 批量插入专家
	 * @param rows
	 * @return
	 */
	@Query("UNWIND {rows} as row merge(n:expert{fId:row.fId}) ON CREATE set"
			+ " n.fId = row.fId, "
			+ " n.name = row.name "
			+ " return ID(n) as id")
	List<Long> saveBetch(@Param("rows") List<Neo4jExpert> rows);
	
	/**
	 * 批量导入专家和图书的关系 
	 *  merge(e)-[r:write1]->(f)
	 */
	@Query("UNWIND {rows} as row "
			+ " match(e:expert{fId:row.leftId}),(f:book{fId:row.rightId}) "
			+ " merge(e)-[r:write1]->(f) return 0 " )
	List<Long> saveBetchRelationBook(@Param("rows") List<Relation> rows);
	
	/**
	 * 批量导入专家和论文的关系 
	 *  merge(e)-[r:publish1]->(f)
	 */
	@Query("UNWIND {rows} as row "
			+ " match(e:expert{fId:row.leftId}),(f:paper{fId:toInt(row.rightId)}) "
			+ " merge(e)-[r:publish1]->(f) return 0 " )
	List<Long> saveBetchRelationPaper(@Param("rows") List<Relation> rows);
	
	/**
	 * 批量导入专家和专利的关系 
	 *  merge(e)-[r:invention]->(f)
	 */
	@Query("UNWIND {rows} as row "
			+ " match(e:expert{fId:row.leftId}),(f:patents{fId:row.rightId}) "
			+ " merge(e)-[r:invention]->(f) return 0 " )
	List<Long> saveBetchRelationPatents(@Param("rows") List<Relation> rows);
	
	/**
	 * 批量导入专家和领域的关系
	 * 作为节点保存
	 */
	@Query("UNWIND {rows} as row "
			+ " merge(n:RelationEfo{expertId:row.leftId,fosId:row.rightId}) "
			+ " return ID(n) as id")
	List<Long> saveBetchRelationField(@Param("rows") List<Relation> rows);
	
	/**
	 * 批量导入专家和领域的关系上的paperCount
	 * 作为节点保存
	 */
	@Query("UNWIND {rows} as row "
			+ " match(n:RelationEfo{expertId:row.leftId,fosId:row.rightId}) "
			+ " set n.paperCount = row.paperCount "
			+ " return ID(n) as id")
	List<Long> saveBetchRelationFieldPaperCount(@Param("rows") List<Relation> rows);
	
	
	/**
	 * 批量导入专家和领域的关系上的rank
	 * 作为节点保存
	 */
	@Query("UNWIND {rows} as row "
			+ " match(n:RelationEfo{expertId:row.leftId,fosId:row.rightId}) "
			+ " set n.rank = row.rank "
			+ " return ID(n) as id")
	List<Long> saveBetchRelationFieldRank(@Param("rows") List<Relation> rows);
	
	/**
	 * 批量导入专家和合作机构的关系
	 * 作为节点保存
	 */
	@Query("UNWIND {rows} as row "
			+ " merge(n:RelationEor{expertId:row.leftId,orgId:row.rightId}) "
			+ " return ID(n) as id")
	List<Long> saveBetchRelationOrg(@Param("rows") List<Relation> rows);
	
}
